package com.ontheroad.mysql.path.service;

import cn.modoumama.page.PageObject;
import cn.modoumama.service.base.impl.BaseServiceImpl;
import com.ontheroad.mysql.path.mapper.PathSystemMapper;
import com.ontheroad.mysql.path.mapper.PoiMapper;
import com.ontheroad.path.model.PathSystemModel;
import com.ontheroad.path.model.PathUserModel;
import com.ontheroad.path.model.PoiModel;
import com.ontheroad.path.service.PathSysService;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Rectangle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kedong on 2017/7/30 0030.
 */
@Service
@Transactional
public class PathSysServiceImpl extends BaseServiceImpl<PathSystemModel, Long> implements PathSysService {

    @Autowired
    private PoiMapper poiMapper;

    @Autowired
    private PathSystemMapper mapper;

    @Autowired
    public void setMapper(PathSystemMapper mapper) {
        setGenericMapper(mapper);
    }

    @Override
    public PoiModel savePoi(PoiModel poi) {
        poiMapper.insertModel(poi);
        return poi;
    }

    @Override
    public void updatePoi(PoiModel poi) {
        poiMapper.updateModelById(poi);
    }

    @Override
    public PathSystemModel save(PathSystemModel systemModel) {
        mapper.insertModel(systemModel);
        return systemModel;
    }

    @Override
    public void deleteSysPathPointByPathId(Long pathId) {
        mapper.deleteSysPathPointByPathId(pathId);
    }

    @Override
    public PoiModel findPoiById(Long id) {
        return poiMapper.getObjectById(id);
    }

    @Override
    public PathSystemModel findByTimestamp(Long timestamp) {
        return mapper.findByTimestamp(timestamp);
    }

    @Override
    public PoiModel findPoiByTimestamp(Long timestamp) {
        return poiMapper.findPoiByTimestamp(timestamp);
    }

    @Override
    public List<PathSystemModel> findNearPath(Double lat, Double lng) {
        // 收缩半径范围 1公里
        int radius = 1;
        SpatialContext geo = SpatialContext.GEO;
        Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(
                geo.makePoint(lng, lat), radius * DistanceUtils.KM_TO_DEG, geo, null);
        double lngMin = rectangle.getMinX();
        double lngMax = rectangle.getMaxX();
        double latMin = rectangle.getMinY();
        double latMax = rectangle.getMaxY();

        // 分页
        PageObject po = new PageObject();
        RowBounds rowBounds = new RowBounds(po.getOffset(), po.getPageSize());
        // 查新轨迹ID
        List<Integer> userPathIds = mapper.findNearPath(lngMin, lngMax, latMin, latMax, rowBounds);
        List<PathSystemModel> systemModels = new ArrayList<>();
        if (userPathIds != null && userPathIds.size() > 0) {
            // 根据ID查询轨迹
            systemModels = mapper.findPathByIds(userPathIds);
        }
        return systemModels;
    }
}
